package com.leetCode.easy;

/**
 * 给你一个整数 n，请你帮忙计算并返回该整数
 * 「各位数字之积」与「各位数字之和」的差
 */
public class SubtractProductAndSum {

    public static void main(String[] args) {
        int i = subtractProductAndSum(234);
        System.out.println("i = " + i);
    }

    public static int subtractProductAndSum(int n) {
        int add = 0, mul = 1;
        while (n > 0) {
            int a = n % 10;
            n = n / 10;
            add += a;
            mul *= a;
        }
        return mul - add;
        //获取这个整数的每位数
//        if (n == 0) {
//            return 0;
//        }
//
//        int pro = pro(n);
//        int sum = sum(n);
//        return pro - sum;

    }
    //取出个位数
    public static int allNum(int n) {

        if (n % 10 > 0) {
            return n % 10;
        }
        return 0;
    }

    //整数相乘
    public static int pro(int n) {
        int pro = 1;
        int p = n;
        while (true) {
            if (p == 0) {
                break;
            } else {
                pro *= allNum(p);
                p = p / 10;
            }
        }
        return pro;
    }

    //整数相加
    public static int sum(int n) {
        int sum = 0;
        int s = n;
        while (true) {
            if (s == 0) {
                break;
            } else {
                sum += allNum(s);
                s = s / 10;
            }
        }
        return sum;
    }
}
